<template>
  <div class="app-container">
    <!-- <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
      >
      <el-form-item label="轮播图位置" prop="advertType">
            <el-select v-model="queryParams.advertType" placeholder="请选择" clearable>
              <el-option
                v-for="dict in dict.type.swiper_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form> -->
    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" width="55" prop="id" />
      <el-table-column label="所属模块" align="center"  width="120" prop="title" />
      <el-table-column
        label="内容"
        align="center"
        prop="content"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <div v-if="scope.row.id == 1">
             {{ scope.row.phone }}
          </div>
          <div v-else>
             {{ scope.row.content }}
          </div>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="180"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['advertising:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属模块" prop="title">
          <el-input v-model="form.title" disabled placeholder="请输入商家名字" />
        </el-form-item>
        <el-form-item label="客服电话" prop="phone"  v-if="form.id == 1">
          <el-input maxlength="11" v-model="form.phone"  placeholder="请输入客服电话" />
        </el-form-item>
        <el-form-item label="内容" prop="content" v-else>
          <Editor v-model="form.content"></Editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, listInfo, editList } from "@/api/zhao/system";
import Upimg from "../../../components/UploadImg";
export default {
  name: "Post",
  dicts: ["sys_normal_disable", "swiper_status"],
  components: { Upimg },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [
          { required: true, message: "手机号不能为空", trigger: "change" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "change" }
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    imgUrlfun3(e) {
      if (e) {
        this.form.images = e.path;
      } else {
        this.form.images = "";
      }
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      getList(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.postId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "修改内容";
      this.isshow = true;
      listInfo({ id: row.id }).then((res) => {
        this.form = res.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.isshow) {
            editList(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form);
          }
        }
      });
    },
   // 表格中优惠面额校验
   tableParValueChange(row) {
      if (row.content === '') {
        row.error = '手机号不能为空'
      } else if (row.parValue < 1) {
        row.error = '优惠面额最小为1'
      } else if (row.parValue > row.salePrice) {
        row.error = '不能大于' + row.salePrice
      } else {
        row.error = ''
      }
    },

  },
};
</script>
<style>
.img {
  width: 120px;
  height: 80px;
  border-radius: 10px;
}
</style>
